/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2019. All rights reserved.
 */

package com.huawei.codecheck.uccp.plugin.utils.http;

import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

/**
 * http 异常重试
 *
 * @author g00514965
 * @since 2019-09-23
 */
public class ExceptionHttpRetryHandler implements HttpRequestRetryHandler {
    private static final int HTTP_EXCEPTION_RETRY_TIMES = 30;

    /**
     * http请求重试
     *
     * @param ex http出现的异常
     * @param executionCount 请求次数
     * @param httpContext httpContext
     * @return 是否成功
     */
    @Override
    public boolean retryRequest(IOException ex, int executionCount, HttpContext httpContext) {
        if (executionCount >= HTTP_EXCEPTION_RETRY_TIMES) {
            // Do not retry if over max retry count
            return false;
        }
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
}
